insert overwrite table jms_dm.dm_unloading_statistics_sum_dt partition (dt)
select unloading_date,
       min(manage_regional_code)                                         as manage_regional_code,
       min(manage_regional_name)                                         as manage_regional_name,
       min(agent_code)                                                   as agent_code,
       min(agent_name)                                                   as agent_name,
       unloading_sitecode,
       min(unloading_sitename)                                           as unloading_sitename,
       count(1)                                                          as unloading_package_sum,
       sum(case when flight_num is not null then 1 else 0 end)           as cycle_bagging_sum,
       sum(case when flight_num is null then 1 else 0 end)               as uncycle_bagging_sum,
       sum(package_num)                                                  as package_num_sum,
       sum(case when flight_num is not null then package_num else 0 end) as cycle_bagging_package_sum,
       unloading_date                                                    as dt
from jms_dm.dm_unloading_statistics_dt
where dt between date_add('{{ execution_date | cst_ds }}',-2) and '{{ execution_date | cst_ds }}'
group by unloading_sitecode, unloading_date
 distribute by dt;